package com.ccc.mq.dynamic.config;

import com.ccc.mq.dynamic.pojo.RocketTopicMsgVO;
import com.ccc.mq.dynamic.service.RoketConsumerService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 消费者相关配置
 */
@Configuration
public class ConsumerConfig {

    @Autowired
    private RoketConsumerService roketConsumerService;

    /**
     * 存储消费者对象，可以扩展为redis
     */
    public static Map<Long, DefaultMQPushConsumer> consumerMap = new ConcurrentHashMap<>();


    @PostConstruct
    public void initRocketConsumers() {
        List<RocketTopicMsgVO> list = roketConsumerService.list();
        list.forEach(vo -> {
            roketConsumerService.startConsumer(vo);
        });
    }
}